package org.softmed.ATComm.server;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.restlet.Restlet;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.softmed.ATComm.ATControlFacade;
import org.softmed.ATComm.sms.SMS;
import org.softmed.ATComm.sms.SMSBatch;
import org.softmed.ATComm.sms.SMSXMLConverter;

import com.thoughtworks.xstream.XStream;

public class SMSRestlet extends Restlet {

	private ATControlFacade atControl;
	SMSXMLConverter converter = new SMSXMLConverter();

	public SMSRestlet(ATControlFacade atControl) {
		this.atControl = atControl;
	}

	@Override
	public void handle(Request request, Response response) {
		System.out.println("received SMS request");

		try {
			String methodName = request.getMethod().getName();
			if (methodName.equals("PUT") || methodName.equals("POST")) {

				Representation entity = request.getEntity();

				// System.out.println("Media Type received :" +
				// entity.getMediaType());

				if (entity == null) {
					// TODO read parameter from http method and send message
					response
							.setEntity(
									"ERROR SENDING SMS VIA PARAMETERS NOT IMPLEMENTED YET ",
									MediaType.TEXT_PLAIN);
					return;

				} else if (entity.getMediaType().includes(MediaType.TEXT_PLAIN)) {
					String sms = entity.getText();
					int index = sms.indexOf('\n');
					String number = sms.substring(0, index);
					String message = sms.substring(index + 1);
					String result = sendSMS(number, message);
					response.setEntity(result, MediaType.TEXT_PLAIN);
					return;
				} else if (entity.getMediaType().includes(MediaType.TEXT_XML)
						|| entity.getMediaType().includes(
								MediaType.APPLICATION_XML)) {
					String xml = entity.getText();
					Object object = converter.fromXML(xml);
					if (object instanceof SMS) {
						SMS sms = (SMS) object;
						String result = sendSMS(sms.getNumber(), sms
								.getMessage());
						response.setEntity(result, MediaType.TEXT_PLAIN);
						// return;
					} else if (object instanceof SMSBatch) {
						SMSBatch batch = (SMSBatch) object;
						List<SMS> list = batch.getSmsList();
						atControl.send(batch);
						return;
					} else {
						response.setEntity("ERROR ON XML REQUEST",
								MediaType.TEXT_PLAIN);
						return;
					}
				} else {
					response.setEntity("ERROR UNSUPPORTED MEDIA TYPE "
							+ entity.getMediaType(), MediaType.TEXT_PLAIN);
					return;
				}
			} else {
				response.setEntity("ERROR UNSUPPORTED HTTP METHOD "
						+ methodName, MediaType.TEXT_PLAIN);
				return;
			}

		} catch (Throwable t) {
			t.printStackTrace();
			response.setEntity("EXCEPTION ERROR " + t.getMessage(),
					MediaType.TEXT_PLAIN);
			return;
		}

		response.setEntity("SUCESS", MediaType.TEXT_PLAIN);

	}

	protected String sendSMS(String number, String message) throws IOException {
		System.out.println("Trying to send SMS : N: " + number + " msg : "
				+ message);
		try {
			atControl.sendSMS(number, message);
		} catch (Throwable e) {
			e.printStackTrace();

			return "ERROR ! " + e.getMessage() + new Date().toLocaleString();
		}
		return "SMS sent sucessfully";
	}
}
